package org.example.config;


import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @BelongsProject：sky-take-out
 * @CreateTime 2024-02-26-14:44
 * @Author SweetPig
 */
@Data
@Slf4j
@AllArgsConstructor
public class MinioUtil {

    private String endpoint;
    private String bucketName;
    private String accessKeyId;
    private String accessKeySecret;


    public String upload(MultipartFile file) throws Exception {
        log.info("文件上传:{}", file);
        //获取文件名称
        String originalFilename = file.getOriginalFilename();
        //文件重命名
        String fileName = UUID.randomUUID() + "." + originalFilename.split("\\.")[1];
        log.info("新文件名称:{}", fileName);
            MinioClient.builder()
                    .endpoint(endpoint)
                    .build()
                    .putObject(
                            PutObjectArgs.builder()
                                    .bucket(bucketName)
                                    .object(fileName)
                                    .stream(file.getInputStream(), file.getSize(), -1)
                                    .build()
                    );

        return endpoint + "/" + bucketName + "/" + fileName;
    }


}
